# Maintainer: Philippe Weyland <philippe.weyland@libertysurf.fr>

_realname=gmic
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.2
pkgrel=9
pkgdesc="A Full-Featured Open-Source Framework for Image Processing (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://gmic.eu"
license=('spdx:CECILL-2.1' 'spdx:CECILL-C')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-opencv"
         "${MINGW_PACKAGE_PREFIX}-openexr")
source=("${_realname}-${pkgver}.tar.gz::https://gmic.eu/files/source/${_realname}_${pkgver}.tar.gz"
        "0004-gmic-core-cmake-build.patch"::https://github.com/dtschump/gmic/commit/7e97e786295f925256b1b1b7349aa3cb16bdfd3b.diff)
sha256sums=('68acec32c45d56fb0b0408acec4f63166171816d70722d63106787f1e7d17030'
            'cdd3c4985d4996d0e103064cc1dfe2dd390fb26afc82f7086481f680a2ae2688')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0004-gmic-core-cmake-build.patch"
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_LIB=ON \
    -DBUILD_LIB_STATIC=OFF \
    -DBUILD_BASH_COMPLETION=OFF \
    -DENABLE_FFMPEG=ON \
    -DENABLE_FFTW=ON \
    -DENABLE_GRAPHICSMAGICK=ON \
    -DENABLE_JPEG=ON \
    -DENABLE_OPENCV=ON \
    -DENABLE_OPENEXR=ON \
    -DENABLE_OPENMP=ON \
    -DENABLE_PNG=ON \
    -DENABLE_TIFF=ON \
    -DENABLE_ZLIB=ON \
    -DENABLE_DYNAMIC_LINKING=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
